<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.w3.org/1999/xhtml">
<head>
    <meta charset="UTF-8">
    <title>注册用户</title>
</head>
<link type="text/css" rel="stylesheet" th:href="@{~/bootstrap-3.3.7/css/bootstrap.css}"/>
<script type="text/javascript" th:src="@{~/bootstrap-3.3.7/js/jquery.js}"></script>
<script type="text/javascript" th:src="@{~/bootstrap-3.3.7/js/bootstrap.js}"></script>
<body>

<script>
    function chakan() {

        var username = $("#username").val();
        $.post(
            "/user/queryUserSize",
            {
                loginName: username
            },
            function (data) {
                if (data > 0) {
                    $("[name='status']").val(1);
                    return false;
                } else {
                    $("[name='status']").val(0);
                }
            }
        );
    }

    function zhuce() {
        var ststus1 = $("[name='status']").val();
        if (ststus1 != 0) {
            alert("存在重复的用户名");
            return;
        }
        var username = $("#username").val();
        var name = $("#name").val();
        var age = $("#age").val();
        var sex = $("#sex").val();
        var phone = $("#phone").val();
        var password = $("#password").val();

        $.post(
            "/user/addUser",
            {
                loginName: username,
                password: password,
                name: name,
                age: age,
                phone: phone,
                sex: sex
            },
            function (data) {
                if (data == 1) {
                    alert("成功");
                    location.href = "/login";
                } else {
                    alert("失败");
                }
            }
        );


    }
</script>

<div class="container">
    <form class="form-horizontal">
        <h3 align="center">注册用户</h3>
        <div class="row">
            <div class="form-group">
                <label for="name" class="col-sm-2 control-label">姓名:</label>
                <div class="col-sm-5">
                    <input type="text" class="form-control" id="name" name="name">
                </div>
            </div>
        </div>
        <div class="row">
            <div class="form-group">
                <label for="username" class="col-sm-2 control-label">用户名:</label>
                <div class="col-sm-5">
                    <input type="text" class="form-control" id="username" name="username" onblur="chakan()">
                    <input type="hidden" name="status" value="0">
                </div>
            </div>
        </div>
        <div class="row">
            <div class="form-group">
                <label for="password" class="col-sm-2 control-label">密码:</label>
                <div class="col-sm-5">
                    <input type="password" class="form-control" id="password" name="password">
                </div>
            </div>
        </div>
        <div class="row">
            <div class="form-group">
                <label class="col-sm-2 control-label">性别:</label>
                <div class="col-sm-5">
                    <label class="radio-inline">
                        <input type="radio" name="sex" value="1">男
                    </label>
                    <label class="radio-inline">
                        <input type="radio" name="sex" value="2">女
                    </label>
                </div>
            </div>
        </div>
        <div class="row">
            <div class="form-group">
                <label for="age" class="col-sm-2 control-label">年龄</label>
                <div class="col-sm-5">
                    <input type="text" class="form-control" id="age" name="age">
                </div>
            </div>
        </div>
        <div class="row">
            <div class="form-group">
                <label for="phone" class="col-sm-2 control-label">手机号码:</label>
                <div class="col-sm-5">
                    <input type="text" class="form-control" id="phone" name="phone">
                </div>
            </div>
        </div>
        <div class="row">
            <div class="form-group">
                <div class="col-sm-offset-2 col-sm-10">
                    <a class="btn btn-default" onclick="zhuce()">注册用户</a>
                </div>
            </div>
        </div>
    </form>
</div>


</body>
</html>